/**
 * Copyright 2013 ABSir's Studio
 * 
 * All right reserved
 *
 * Create on 2013-6-17 下午4:26:43
 */
package com.absir.android.orm;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import android.app.Application;

import com.absir.bean.basis.BeanDefine;
import com.absir.bean.basis.BeanScope;
import com.absir.bean.config.IBeanDefineSupply;
import com.absir.bean.config.IBeanTypeFilter;
import com.absir.bean.core.BeanFactoryImpl;
import com.absir.bean.inject.value.Bean;
import com.absir.bean.inject.value.Inject;
import com.absir.core.kernel.KernelClass;
import com.absir.core.kernel.KernelObject;
import com.j256.ormlite.table.DatabaseTable;

/**
 * @author absir
 * 
 */
@Bean(scope = BeanScope.LAZYINIT)
public class DaoFactory implements IBeanTypeFilter, IBeanDefineSupply {

	/** Database_Name */
	static final String Database_Name = "absir.db";

	/** tableTypes */
	static List<Class<?>> Table_Types = new ArrayList<Class<?>>();

	/** application */
	@Inject
	private Application application;

	/** daoHelper */
	private DaoHelper daoHelper;

	/** forceCopy */
	private boolean forceCopy;

	/**
	 * @return
	 */
	public DaoHelper getDaoHelper() {
		if (daoHelper == null) {
			try {
				copyDatabase();

			} catch (IOException e) {
			}

			daoHelper = new DaoHelper(application, Database_Name);
		}

		return daoHelper;
	}

	/**
	 * @throws IOException
	 * 
	 */
	private void copyDatabase() throws IOException {
		File destFile = new File("//data/data//" + application.getPackageName() + "/databases/" + Database_Name);
		if (destFile.isFile() && destFile.exists() && !forceCopy) {
			return;
		}

		Class<?> rawClass = KernelClass.forName(application.getPackageName() + ".R" + "$" + "raw");
		if (rawClass == null) {
			return;
		}

		Integer absir = (Integer) KernelObject.declaredGet(rawClass, "absir");
		if (absir != null) {
			InputStream inputStream = null;
			OutputStream outputStream = null;
			try {
				inputStream = application.getResources().openRawResource(absir);
				outputStream = FileUtils.openOutputStream(destFile);
				IOUtils.copyLarge(inputStream, outputStream);

			} finally {
				if (inputStream != null) {
					inputStream.close();
				}

				if (outputStream != null) {
					outputStream.close();
				}
			}

		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.absir.core.kernel.KernelList.Orderable#getOrder()
	 */
	@Override
	public int getOrder() {
		// TODO Auto-generated method stub
		return -1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.absir.bean.config.IBeanTypeFilter#match(java.lang.Class)
	 */
	@Override
	public boolean match(Class<?> beanType) {
		// TODO Auto-generated method stub
		return beanType.getAnnotation(DatabaseTable.class) != null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.absir.bean.config.IBeanDefineSupply#getBeanDefines(com.absir.bean
	 * .core.BeanFactoryImpl, java.lang.Class)
	 */
	@Override
	public List<BeanDefine> getBeanDefines(BeanFactoryImpl beanFactory, Class<?> beanType) {
		// TODO Auto-generated method stub
		if (beanType.getAnnotation(DatabaseTable.class) != null) {
			Table_Types.add(beanType);
		}

		return null;
	}

}
